/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codemanager.listeners.action;

import codemanager.CodeManager;
import codemanager.database.dao.impl.ProjectDAOImpl;
import codemanager.database.pojo.Project;
import codemanager.utility.ContextUtil;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

/**
 *
 * @author undefined
 */
public class CMMouseListener implements MouseListener{
 
    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println(e.getComponent());
        if(e.getButton()==3){
            if(1<((DefaultMutableTreeNode)((JTree)(e.getSource())).getLastSelectedPathComponent()).getLevel()){
                JPopupMenu jpm = new JPopupMenu();
                JMenuItem jmenu1 = new JMenuItem("打开");
                JMenuItem jmenu2 = new JMenuItem("输出到...");
                jmenu1.setActionCommand("open");
                jmenu1.addActionListener(new CMActionListener());
                jmenu2.setActionCommand("output");
                jmenu2.addActionListener(new CMActionListener());
                jpm.add(jmenu1);
                jpm.add(jmenu2);
                jpm.show(e.getComponent(), e.getX(), e.getY());
            }
        }
        if(2==e.getClickCount()&&e.getButton()==1){
            if(((DefaultMutableTreeNode)((JTree)(e.getSource())).getLastSelectedPathComponent()).isLeaf()){
            TreeNode[] nodes = ((DefaultMutableTreeNode)(((JTree)(e.getSource())).getLastSelectedPathComponent())).getPath();
            //获取文件路径
            StringBuffer path = new StringBuffer();
            for(int i=0;i<nodes.length;i++){
              if(1<i){
                if(2==i){
                    ProjectDAOImpl projectDAOImpl = new ProjectDAOImpl();
                    List<Project> list = projectDAOImpl.queryProjectsByName(nodes[2].toString());
                    path.append(((Project)(list.get(0))).getProURL());
                }else{
                  path.append("\\");
                  path.append(nodes[i].toString());
              }
            }
            }
            CodeManager codeManager = (CodeManager)((JTree)(e.getSource())).getParent().getParent().getParent().getParent().getParent().getParent().getParent();
            codeManager.addTabToRight(path.toString());
            }
        }
    }
    @Override
    public void mousePressed(MouseEvent e) {
       
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
    
}
